#!/bin/sh
# statis日志中,initial接口参数去重,mysql中计算guid,导入mongoDB中

if [ ! -d "$1" ] || [ ! -n "$2" ]; then
cat<<HELP
Usage: ./statis_guid_import.sh sourcePath date
Example: 
1. specified date: 
    ./statis_guid_import.sh /data/log 20120113
2. specified date hour: 
    ./statis_guid_import.sh /data/log 20120113 06
HELP
    exit 1
fi

PWD="/opt/wanalytics/scripts"
TOOLS=`dirname $PWD`/Tools

YEAR=${2:0:4}
MONTH=${2:4:2}
DAY=${2:6:2}
HOUR=$3

if [ -z "$HOUR" ]; then
    HOUR="[0-9][0-9]"
fi

# 日志路径
#LOGS=$PWD/log/statis_guid_import.log

SOURCE_PATH=$1/$YEAR/$MONTH/$DAY
if [ ! -d "$SOURCE_PATH/_import" ]; then
    mkdir -p $SOURCE_PATH/_import
fi

# 此日志为1整天,按小时请改后缀名
GUID_MYSQL_IMPORT=$SOURCE_PATH/_import/guid_mysql_import_all

if [ -s $GUID_MYSQL_IMPORT ]; then
    rm -rf $GUID_MYSQL_IMPORT
fi

echo "cat $SOURCE_PATH/[a-z,0-9]*/statis_$HOUR |awk '$7 == 200 && $4 ~ /^\"(\/openapi-wireless)?\/initial\.?(json|xml|text)?/ { Query = ($5 ~ /^\"-\"$/) ? $6 : $5; gsub(/\042/,"",Query); print $2,Query;}' | sort -u -k2 | awk -f $PWD/statis_guid_import.awk -v Outfile=$GUID_MYSQL_IMPORT"

cat $SOURCE_PATH/[a-z,0-9]*/statis_$HOUR |awk '$7 == 200 && $4 ~ /^\"(\/openapi-wireless)?\/initial\.?(json|xml|text)?/ { Query = ($5 ~ /^\"-\"$/) ? $6 : $5; gsub(/\042/,"",Query); print $2,Query;}' | sort -u -k2 | awk -f $PWD/statis_guid_import.awk -v Outfile=$GUID_MYSQL_IMPORT

scp $GUID_MYSQL_IMPORT root@10.103.13.103:/tmp/

date_start=`date "+%s"`
python $PWD/statis_guid_import.py /tmp/guid_mysql_import_all

date_end=`date "+%s"`
time=`expr "$date_end" - "$date_start"`
echo "clientUser import take $time seconds"
